<?php

namespace app\common\util;

/**
 * 平台端的管理员权限
 */
class AdminPower
{
    /**
     * 系统配置
     * @param string $apCodes
     * @return array
     */
    public static function getPowerSys($apCodes = "")
    {
        $powerArr = array(
            //========= 一级 账号权限
            ['id' => 'model_admin', 'pId' => '', 'name' => '账号权限'],
            //二级
            ['id' => 'Admin', 'pId' => 'model_admin', 'name' => '管理员'],
            ['id' => 'Admin' . '_' . 'index', 'pId' => 'Admin', 'name' => '查询'],
            ['id' => 'Admin' . '_' . 'create', 'pId' => 'Admin', 'name' => '添加'],
            ['id' => 'Admin' . '_' . 'update', 'pId' => 'Admin', 'name' => '编辑'],
            ['id' => 'Admin' . '_' . 'delete', 'pId' => 'Admin', 'name' => '删除'],
            ['id' => 'Admin' . '_' . 'resetPassword', 'pId' => 'Admin', 'name' => '重置密码'],

            ['id' => 'AdminRole', 'pId' => 'model_admin', 'name' => '管理员角色'],
            ['id' => 'AdminRole' . '_' . 'index', 'pId' => 'AdminRole', 'name' => '查询'],
            ['id' => 'AdminRole' . '_' . 'create', 'pId' => 'AdminRole', 'name' => '添加'],
            ['id' => 'AdminRole' . '_' . 'update', 'pId' => 'AdminRole', 'name' => '编辑'],
            ['id' => 'AdminRole' . '_' . 'delete', 'pId' => 'AdminRole', 'name' => '删除'],

            ['id' => 'AdminPost', 'pId' => 'model_admin', 'name' => '管理员岗位'],
            ['id' => 'AdminPost' . '_' . 'index', 'pId' => 'AdminPost', 'name' => '查询'],
            ['id' => 'AdminPost' . '_' . 'create', 'pId' => 'AdminPost', 'name' => '添加'],
            ['id' => 'AdminPost' . '_' . 'update', 'pId' => 'AdminPost', 'name' => '编辑'],
            ['id' => 'AdminPost' . '_' . 'delete', 'pId' => 'AdminPost', 'name' => '删除'],

            // 活动管理
            ['id' => 'lottory', 'pId' => '', 'name' => '抽奖活动'],
            ['id' => 'LotteryActivity', 'pId' => 'lottory', 'name' => '活动管理'],
            ['id' => 'LotteryActivity' . '_' . 'index', 'pId' => 'LotteryActivity', 'name' => '查询'],
            ['id' => 'LotteryActivity' . '_' . 'add', 'pId' => 'LotteryActivity', 'name' => '添加'],
            ['id' => 'LotteryActivity' . '_' . 'edit', 'pId' => 'LotteryActivity', 'name' => '编辑'],
            ['id' => 'LotteryActivity' . '_' . 'detail', 'pId' => 'LotteryActivity', 'name' => '详情'],
            ['id' => 'LotteryActivity' . '_' . 'delete', 'pId' => 'LotteryActivity', 'name' => '删除'],
            //活动管理结束
            //============== 一级 运营管理
            ['id' => 'model_business', 'pId' => '', 'name' => '运营管理'],
            //------------- 用户
            ['id' => 'User', 'pId' => 'model_business', 'name' => '用户'],
            ['id' => 'User' . '_' . 'index', 'pId' => 'User', 'name' => '查询'],
            ['id' => 'User' . '_' . 'resetPassword', 'pId' => 'User', 'name' => '重置密码'],
            ['id' => 'User' . '_' . 'bindPass', 'pId' => 'User', 'name' => '绑定审核'],
            ['id' => 'User' . '_' . 'delete', 'pId' => 'User', 'name' => '删除用户'],


            //===============一级 系统设置
            ['id' => 'model_sys', 'pId' => '', 'name' => '系统设置'],
            ['id' => 'Config' . '_' .'web', 'pId' => 'model_sys', 'name' => '基础配置'],
            ['id' => 'Config' . '_' .'wechat_xcx', 'pId' => 'model_sys', 'name' => '小程序设置'],


            //一级
            ['id' => 'model_log', 'pId' => '', 'name' => '系统日志'],
            //二级
            ['id' => 'AdminLog', 'pId' => 'model_log', 'name' => '日志'],
            ['id' => 'AdminLog_index', 'pId' => 'AdminLog', 'name' => '查询'],
            ['id' => 'AdminLog_detail', 'pId' => 'AdminLog', 'name' => '详情'],
        );
        if (!empty($apCodes)) {
            foreach ($powerArr as $k => $v) {
                if (strpos($apCodes, ',' . $v ['id'] . ',') !== false) {
                    $powerArr[$k]['checked'] = true;
                }
            }
        }
        return $powerArr;
    }

    /**
     * 获取权限列表
     * @return array
     */
    public static function getPowerAssign()
    {
        $res = self::getPowerList('', self::getPowerSys());

        return $res;
    }

    /**
     * 整理权限列表
     * @param $pkey
     * @param $power
     * @return array
     */
    private static function getPowerList($pkey, $power)
    {
        $resData = array();
        foreach ($power as $k => $v) {
            if ($pkey === $v['pId']) {
                unset($power[$k]);
                if ($pkey) {
                    $resData['children'][] = array_merge($v, self::getPowerList($v['id'], $power));
                } else {
                    $resData[] = array_merge($v, self::getPowerList($v['id'], $power));
                }
            }
        }
        return $resData;
    }
}
